<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Feiliu
 *
 * @author dragon.hu add @2016.3.1
 */
require_once 'BaseChannel.php';

//飞流
class Feiliu implements BaseChannel 
{
	
	function __construct() 
	{
		//echo "construct";
	}

	//put your code here
	function android($data) 
	{
		$paymentKey=""; //
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) 
	{
		$paymentKey=""; //
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		require_once ('include/feiliu/lib/Rsalib.php');
		$orderLog = new ChargeOrder();
		//$orderLog->log("feiliu", $_REQUEST);
		$request = file_get_contents("php://input");
		$orderLog->log("feiliu", $request);
		$data = json_decode($request, true);
		
		$rsa = new Rsalib();
		$decryptdata = $rsa->pubDecryptByCompanyId($data['verifystring'],100155);
		/*
		 * 1.请采用解密后用明文进行比较。支付结果通知强烈建议验证,未 防止第三方篡改数据而造成不必要的损失。
		 * 2.如果订单状态为支付成功（支付状态码会单独提供）则由cp执行游侠相关逻辑
		 **/
		
		//返回pay平台通知结果,无论是否支付成功都要返回消息结果
		$getstr = $data['florderid'].'|'.$data['orderid'].'|'.$data['productid'].'|'.
				$data['cardno'].'|'.$data['amount'].'|'.$data['ret'].'|'.$data['cardstatus'].'|'.$data['merpriv'];
		
		if ($getstr == $decryptdata)
		{
			$tmpstatus = STATUS_SUCCESS;
			if ($data['ret'] != 1)
			{
				$tmpstatus = STATUS_FAILD;
			}
			//进行发货
			if ($platform == ANDROID)
			{
				if(false == $orderLog->recordOrder($tmpstatus, $data['amount']/100, $data['orderid'], "feiliu", $platform,$data['florderid']))
				{
					$result['code']="1";
					$result['tips']=urlencode("接受失败");
					$orderLog->log("feiliu",array("platform"=> $platform,"order"=>$data['orderid'],"money"=>$data['amount'],"serverresult"=>"faild"));
				}else
				{
					//支付成功
					$result['code']="0";
					$result['tips']=urlencode("接受成功");
					$orderLog->log("feiliu",array("platform"=> $platform,"order"=>$data['orderid'],"money"=>$data['amount'],"serverresult"=>"success"));
				}
			}
			else if ($platform == IOS)
			{
				if(false == $orderLog->recordOrder($tmpstatus, $data['amount']/100, $data['orderid'], "IosFeiLiu", $platform,$data['florderid']))
				{
					$result['code']="1";
					$result['tips']=urlencode("接受失败");
					$orderLog->log("feiliu",array("platform"=> $platform,"order"=>$data['orderid'],"money"=>$data['amount'],"serverresult"=>"faild"));
				}else
				{
					//支付成功
					$result['code']="0";
					$result['tips']=urlencode("接受成功");
					$orderLog->log("feiliu",array("platform"=> $platform,"order"=>$data['orderid'],"money"=>$data['amount'],"serverresult"=>"success"));
				}
			}
		}
		else 
		{
			//"$sig验证失败"
			$result['code']="1";
			$result['tips']=urlencode("接受失败");
			$orderLog->log("feiliu","platform: $platform sign verify error");
		}
		$ret = json_encode($result);
		return urldecode($ret);
	}
}